package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.adapter.Chart1Adapter;
import com.hxkcloud.patient.beans.LineListBean;
import com.hxkcloud.patient.beans.MyChartListBean;
import com.hxkcloud.patient.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.StringUtil;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/28.
 */
public class MonitorChart1Activity extends BaseActivity implements View.OnClickListener, Chart1Adapter.ResultSelectedListener, PullToRefreshLayout.OnRefreshListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<LineListBean> chartInfoList;
    private Chart1Adapter adapter;

    private String patientId = "";

    private ObjectResultBean<MyChartListBean> resultBean;

    private boolean isFromList = false;


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETMONITORDAYDATA_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyChartListBean>) msg.obj;
                    if (resultBean != null) {
                        chartInfoList = resultBean.getData().getChartList();
                        adapter.setData(chartInfoList);
//                        toastMsg(resultBean.getMessage());
                    }


                }

                break;
            case MessageCode.NETWORKCODE_GETMONITORDAYDATA_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_chart1);
        if (getIntent().hasExtra("patientId")) {
            patientId = getIntent().getStringExtra("patientId");
        }
        if(getIntent().hasExtra("isFromList")){
            isFromList = getIntent().getBooleanExtra("isFromList",false);
        }
        chartInfoList = new ArrayList<LineListBean>();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setOnClickListener(this);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getString(R.string.medical_record));

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.chart1_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.chart1_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);


        adapter = new Chart1Adapter(this, chartInfoList, recycleView, this, patientId, loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient), screenWidth);
        recycleView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.getMonitorDayData(patientId, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;

        }
    }

    @Override
    public void onSelected(int position, String type) {
        if ("itemClick".equals(type)) {
            StringUtil.zhugeTrack(mContext, "监测-" + chartInfoList.get(position).getName(), "监测中" + chartInfoList.get(position).getName() + "区域点击数");

            Intent intent = new Intent(mContext, ChartDetailActivity.class);
            intent.putExtra("isFromList", true);
            intent.putExtra("patientId", patientId);
            intent.putExtra("kindId", chartInfoList.get(position).getKindId());
            intent.putExtra("name", chartInfoList.get(position).getName());
            intent.putExtra("time", chartInfoList.get(position).getDate());
            intent.putExtra("min", chartInfoList.get(position).getMinValue());
            intent.putExtra("max", chartInfoList.get(position).getMaxValue());
            if (chartInfoList.get(position).getLineList() != null) {
                for (int i = 0; i < chartInfoList.get(position).getLineList().size(); i++) {
                    intent.putExtra("text" + i, chartInfoList.get(position).getLineList().get(i).getName());
                }
            }
            startActivityForResult(intent, 111);
        }


    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .refreshFinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }
}
